﻿/*
 * Alain Metge 2011
 * http://www.dotnetrangers.net
 * See license.txt
 */
using System;
using System.ComponentModel.Composition;
using System.Windows;
using Microsoft.VisualStudio.ExtensionManager;
using Microsoft.VisualStudio.ExtensionManager.UI;
using Microsoft.VisualStudio.ExtensionsExplorer;
using VsxFactory.ExtensionManager.Providers;
using System.Linq;

namespace VsxFactory.ExtensionManager
{
    //[Export(typeof(IVsExtensionManagerDialogProvider))]
    //class UpdatePrivateExtensionManager : PackagesProviderBase,  IVsExtensionManagerDialogProvider
    //{
    //    private IServiceProvider serviceProvider;

    //    public UpdatePrivateExtensionManager() 
    //    {
    //        serviceProvider = new Microsoft.VisualStudio.Shell.ServiceProvider(Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(Microsoft.VisualStudio.OLE.Interop.IServiceProvider)) as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
    //    }

    //    public override string Name
    //    {
    //        get { return "Updates private gallery"; }
    //    }
         
    //    protected override void FillRootNodes()
    //    {
    //        PackagesTreeNodeBase node = null;
    //        IPackageSourceSettingsManager settings = serviceProvider.GetService(typeof(IPackageSourceSettingsManager)) as IPackageSourceSettingsManager;
    //        var extensionManager = serviceProvider.GetService(typeof(Microsoft.VisualStudio.ExtensionManager.SVsExtensionManager)) as Microsoft.VisualStudio.ExtensionManager.IVsExtensionManager;
    //        foreach (PackageSource src in settings.PackageSources)
    //        {
    //            try
    //            {
    //                IPackageRepository repository = new DataServicePackageRepository(new Uri(src.Source + "/VsixGallery.svc"));
    //                node = new UpdateTreeNode(extensionManager, this, src.Name, RootNode, repository);
    //                if (src.Name == settings.ActivePackageSourceString)
    //                    SelectNode(node);
    //            }
    //            catch (Exception)
    //            {
    //                node = new EmptyTreeNode(this, src.Name, RootNode);
    //            }
    //        }

    //        RootNode.Nodes.Add(node);
    //    }       
    //}    

    [Export(typeof(IVsExtensionManagerDialogProvider))]
    class PrivateExtensionManager : PackagesProviderBase,  IVsExtensionManagerDialogProvider
    {
        private IServiceProvider serviceProvider;

        public PrivateExtensionManager() 
        {
            serviceProvider = new Microsoft.VisualStudio.Shell.ServiceProvider(Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(Microsoft.VisualStudio.OLE.Interop.IServiceProvider)) as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
        }

        // Tab name
        public override string Name
        {
            get {return "Private gallery";}
        }

        protected override void FillRootNodes()
        {
            var extensionManager = serviceProvider.GetService(typeof(Microsoft.VisualStudio.ExtensionManager.SVsExtensionManager)) as Microsoft.VisualStudio.ExtensionManager.IVsExtensionManager;

            PackagesTreeNodeBase node = null;
            IPackageSourceSettingsManager settings = serviceProvider.GetService(typeof(IPackageSourceSettingsManager)) as IPackageSourceSettingsManager;
            foreach (PackageSource src in settings.PackageSources)
            {
                try
                {
                    IPackageRepository repository = new DataServicePackageRepository(new Uri(src.Source));
                    node = new SimpleTreeNode(this, src.Name, RootNode, repository);
                    if (src.Name == settings.ActivePackageSourceString)
                        SelectNode(node);
                    RootNode.Nodes.Add(node);
                    node = new UpdateTreeNode(extensionManager, this, src.Name, RootNode, repository);
                    RootNode.Nodes.Add(node);
                    
                }
                catch (Exception)
                {
                    node = new EmptyTreeNode(this, src.Name, RootNode);
                    RootNode.Nodes.Add(node);
                }

            }
        }
    }
}
